﻿using Net;
using Net.AI.Native;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace RecastCsharp
{
	internal class Program
    {
        static unsafe void Main(string[] args)
        {
			var sample = RecastDll.CreateSoloMesh();
			RecastDll.SetBuildSettings(sample, BuildSettings.Default);
			//RecastDll.CollectSettings(sample, BuildSettings.Default);
			//var r = RecastDll.LoadMeshFile(sample, @"D:\\recastnavigation_share\\RecastDemo\\Bin\\Meshes\\dungeon.obj");
			var r2 = RecastDll.LoadNavMesh(sample, "D:/recast-pathfinding/Assets/demo2NavMesh.bin");
			//var text = File.ReadAllText(@"D:\\recastnavigation_share\\RecastDemo\\Bin\\Meshes\\dungeon.obj");
			//var r3 = RecastDll.LoadObjText(sample, text, text.Length);
			//var r1 = RecastDll.Build(sample);

			//using (var stream = new BinaryReader(new FileStream(@"D:\recastnavigation_share\RecastCsharp\bin\Debug\model.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)))
			//{
			//	var vertexCountc = stream.ReadInt32();
			//	var trianglesLen = stream.ReadInt32();
			//	int[] triangles = new int[trianglesLen];
			//	for (int i = 0; i < trianglesLen; i++)
			//		triangles[i] = stream.ReadInt32();
			//	var vertsLen = stream.ReadInt32();
			//	float[] m_verts = new float[vertsLen];
			//	for (int i = 0; i < vertsLen; i++)
			//		m_verts[i] = stream.ReadSingle();
			//	var normalsLen = stream.ReadInt32();

			//	var r2 = RecastDll.LoadMesh(sample, m_verts, triangles, vertexCountc, trianglesLen / 3);
			//	var r1 = RecastDll.Build(sample);
			//}


			float* array = stackalloc float[65535];
			int count;
			RecastDll.GetDrawNavMesh(sample, array, out count);

			//float[] m_spos = new float[] { 19,15,-65 };
			//float[] m_epos = new float[] { 2,9,-5 };

			float* m_spos = stackalloc float[] { -41.1599998f, 6.48999977f, -49.7900009f };
			float* m_epos = stackalloc float[] { 48.2099991f, 12.3400002f, 39.3199997f };

			float* m_straightPath = stackalloc float[256 * 3];

			Stopwatch stopwatch = Stopwatch.StartNew();
			//for (int i = 0; i < 100000; i++)
			{
				RecastDll.FindPathFollow(sample, m_spos, m_epos, m_straightPath, out int outPointCount, dtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS);

				List<Vector3> paths = new List<Vector3>();
				for (int i = 1; i < outPointCount; i++) //为什么不能要最后一条线? 因为后面一条线偶尔出现y=1的问题, 最后一个不要也不影响
				{
					int v = i * 3;
					paths.Add(new Vector3(m_straightPath[v - 3], m_straightPath[v - 2] + 0, m_straightPath[v - 1])); //a线
					paths.Add(new Vector3(m_straightPath[v + 0], m_straightPath[v + 1] + 0, m_straightPath[v + 2])); //b线
				}

			}
			stopwatch.Stop();
			Console.WriteLine("C++ 10万次寻路:" + stopwatch.Elapsed);

			Console.ReadLine();
		}
	}
}